package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.license.entity.CipherLicense;
import com.ruoyi.system.license.utils.LicenseUtil;
import com.ruoyi.system.license.utils.MachineCodeUtil;
import com.ruoyi.system.service.ProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

/**
 * @Author: LiHuaZhi
 * @Description:
 **/
@Api(tags = "加密授权接口管理")
@RestController
@RequestMapping("/cipher")
@Slf4j
@Anonymous
public class CipherController {
    @Value("${license.key}")
    private String keyPath;

    @Value("${license.pri}")
    private String priPath;

    @Value("${license.pub}")
    private String pubPath;

    @Resource
    private ProjectService projectService;


    /**
     * 生成公钥和授权文件-内部接口,实际情况下这两个接口应该独立出来，不会在包里面
     *
     * @return
     */
    @ApiOperation(value = "生成公钥和授权文件", notes = "生成公钥和授权文件")
    @PostMapping("/set")
    public Object setLicense(@RequestBody CipherLicense param) {
//        LicenseUtil.generaLicense(priPath, keyPath, pubPath, param.getStartTime(), param.getEndTime(), param.getLicenseCode());
        String s = projectService.generaLicense("36ca7920d004a572a33a6bd219b24649", param.getEndTime(), param.getLicenseCode());
        Map<String, String> stringStringMap = LicenseUtil.testLicense2(param.getLicenseCode(), s.split("====")[0], s.split("====")[1]);
        return "操作成功";
    }

    /**
     * @return
     */
    @ApiOperation(value = "手动通过公钥和授权文件实现授权验证", notes = "手动通过公钥和授权文件实现授权验证")
    @GetMapping("/load")
    public Object loadLicense(String code) {
        Map<String, String> map = LicenseUtil.testLicense(code, keyPath, pubPath);
        System.out.println(map);
        if (map != null && map.size() > 0) {
            return "操作成功";
        } else {
            return "操作失败";
        }
    }

    @ApiOperation(value = "获取本地BIOS码")
    @GetMapping("/getBiosUuid")
    public AjaxResult getBiosUuid() {
        try {
            return AjaxResult.success(MachineCodeUtil.getBiosUuid());
        } catch (Exception e) {
         throw new RuntimeException("获取机器码失败");
        }
    }

    public static void main(String[] args) throws IOException {
        String biosUuid = MachineCodeUtil.getBiosUuid();
        System.out.println(biosUuid);
    }

}
